import {
  fetchServeCateList, fetchGoodsCateList, fetchServeList, fetchServeCardList, fetchGoodsList, fetchGoodsSpecList,
  fetchPkgList
} from '@/services/mall/MallCommon';
import {arr2tree} from '@/utils/utils';

export default {
  namespace: 'MallCommon',

  state: {
    pkgList: [],
    serveCateList: [],
    serveCateTree: [],
    serveList: [],
    serveCardList: [],
    goodsCateList: [],
    goodsCateTree: [],
    goodsList: [],
    goodsSpecList: [],
  },

  effects: {
    * fetchPkgList({}, {call, put}) {
      const response = yield call(fetchPkgList);
      if (response.code === 0) {
        yield put({
          type: 'chgPkgList',
          payload: response.data.map(d => {
            d.label = `${d.name} #售价:￥${(d.sellPrice / 100).toFixed(2)}^^^库存:${d.stock}^^^id:${d.id}#`;
            d.value = d.id;
            d.key = d.id;
            return d;
          }),
        });
      }
    },

    * fetchServeCateList({}, {call, put}) {
      const response = yield call(fetchServeCateList);
      if (response.code === 0) {
        yield put({
          type: 'chgServeCateList',
          payload: response.data,
        });
        yield put({
          type: 'chgServeCateTree',
          payload: arr2tree(response.data.map(d => {
            d.label = d.name;
            d.value = d.id;
            d.key = d.id;
            return d;
          })),
        });
      }
    },

    * fetchServeList({}, {call, put}) {
      const response = yield call(fetchServeList);
      if (response.code === 0) {
        yield put({
          type: 'chgServeList',
          payload: response.data.map(d => {
            d.label = `${d.name} #售价:￥${(d.sellPrice / 100).toFixed(2)}^^^库存:${d.stock}^^^id:${d.id}#`;
            d.value = d.id;
            d.key = d.id;
            return d;
          }),
        });
      }
    },

    * fetchServeCardList({payload}, {call, put}) {
      const response = yield call(fetchServeCardList, payload);
      if (response.code === 0) {
        yield put({
          type: 'chgServeCardList',
          payload: response.data.map(d => {
            d.label = `${d.name} #售价:￥${(d.sellPrice / 100).toFixed(2)}^^^库存:${d.stock}^^^id:${d.id}#`;
            d.value = d.id;
            d.key = d.id;
            return d;
          }),
        });
      }
    },

    * fetchGoodsCateList({}, {call, put}) {
      const response = yield call(fetchGoodsCateList);
      if (response.code === 0) {
        yield put({
          type: 'chgGoodsCateList',
          payload: response.data,
        });
        yield put({
          type: 'chgGoodsCateTree',
          payload: arr2tree(response.data.map(d => {
            d.label = d.name;
            d.value = d.id;
            d.key = d.id;
            return d;
          })),
        });
      }
    },

    * fetchGoodsList({}, {call, put}) {
      const response = yield call(fetchGoodsList);
      if (response.code === 0) {
        yield put({
          type: 'chgGoodsList',
          payload: response.data.map(d => {
            d.label = `${d.name} #售价:￥${(d.sellPrice / 100).toFixed(2)}^^^库存:${d.stock}^^^id:${d.id}#`;
            d.value = d.id;
            d.key = d.id;
            return d;
          }),
        });
      }
    },

    * fetchGoodsSpecList({payload}, {call, put}) {
      const response = yield call(fetchGoodsSpecList, payload);
      if (response.code === 0) {
        yield put({
          type: 'chgGoodsSpecList',
          payload: response.data.map(d => {
            d.label = `${d.name} #售价:￥${(d.sellPrice / 100).toFixed(2)}^^^库存:${d.stock}^^^id:${d.id}#`;
            d.value = d.id;
            d.key = d.id;
            return d;
          }),
        });
      }
    },

  },

  reducers: {
    chgPkgList(state, action) {
      return {
        ...state,
        pkgList: action.payload,
      };
    },
    chgServeCateList(state, action) {
      return {
        ...state,
        serveCateList: action.payload,
      };
    },
    chgServeCateTree(state, action) {
      return {
        ...state,
        serveCateTree: action.payload,
      };
    },
    chgServeList(state, action) {
      return {
        ...state,
        serveList: action.payload,
      };
    },
    chgServeCardList(state, action) {
      return {
        ...state,
        serveCardList: action.payload,
      };
    },
    chgGoodsCateList(state, action) {
      return {
        ...state,
        goodsCateList: action.payload,
      };
    },
    chgGoodsCateTree(state, action) {
      return {
        ...state,
        goodsCateTree: action.payload,
      };
    },
    chgGoodsList(state, action) {
      return {
        ...state,
        goodsList: action.payload,
      };
    },
    chgGoodsSpecList(state, action) {
      return {
        ...state,
        goodsSpecList: action.payload,
      };
    },

  },

};
